package cn.tedu.lucky.draw.server.controller;


import cn.tedu.lucky.draw.server.common.annotation.OperationLog;
import cn.tedu.lucky.draw.server.common.result.JsonResult;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.protocol.po.Prize;
import cn.tedu.lucky.draw.server.protocol.query.ActivityQuery;
import cn.tedu.lucky.draw.server.service.IActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.batch.core.*;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Api(tags ="活动管理")
public class ActivityController {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job csvToDbJob;

    @Autowired
    private JobExplorer jobExplorer;



    @Autowired
    private IActivityService activityService;



    @PostMapping("/draw/activity/getAll")
    public JsonResult<List<Activity>> findAll(@RequestBody ActivityQuery activityQuery) {
        List<Activity> activities = activityService.findAll(activityQuery);
        return JsonResult.ok(activities);
    }

    @PostMapping("/draw/activity/save")
    @OperationLog(operation = "新增活动")
    public JsonResult<String> save(@RequestBody Activity activity) {
        activityService.save(activity);
        return JsonResult.ok("新增成功");
    }

    @OperationLog(operation = "活动批量导入")
    @GetMapping("/activity/csvToDB")
    public JsonResult<String> csvToDB() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        /**
         * jobExplorer 可以获取到当前运行的任务，以及上次作业的运行结果
         */
        JobParameters jobParameters =
                new JobParametersBuilder(new JobParameters(), jobExplorer)
                        .getNextJobParameters(csvToDbJob).toJobParameters();
        JobExecution run = jobLauncher.run(csvToDbJob, jobParameters);
        Long id = run.getId();
        return JsonResult.ok("任务ID：" + id);
    }

    @PostMapping("/draw/activity/{id}")
    public JsonResult<Activity> getActivityById(@PathVariable("id") Long id) {
        Activity activity = activityService.findActivityById(id);
        return JsonResult.ok(activity);
    }

    @ApiOperation(value = "获取活动列表-APP端")
    @GetMapping("/app/draw/activity/getAll")
    public JsonResult<List<Activity>> getAll(){
        ActivityQuery activityQuery = new ActivityQuery();
        activityQuery.setStatus(1);
        List<Activity> activities = activityService.findAll(activityQuery);
        return JsonResult.ok(activities);
    }

    @GetMapping("/app/draw/activity/trigger")
    @ApiOperation(value = "触发活动抽奖-APP端")
    public JsonResult<Prize> trigger(Long activityId) throws JSONException {
        Prize prize=activityService.trigger(activityId);
        return JsonResult.ok(prize);
    }


}
